1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * gtkD is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with gtkD; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 
20 // generated automatically - do not change
21 // find conversion definition on APILookup.txt
22 // implement new conversion functionalities on the wrap.utils pakage
23 
24 
25 module gtk.ToggleButton;
26 
27 private import glib.ConstructionException;
28 private import glib.Str;
29 private import gobject.ObjectG;
30 private import gobject.Signals;
31 private import gtk.Button;
32 private import gtk.Widget;
33 private import gtk.c.functions;
34 public  import gtk.c.types;
35 private import std.algorithm;
36 
37 
38 /**
39  * A `GtkToggleButton` is a button which remains “pressed-in” when
40  * clicked.
41  * 
42  * Clicking again will cause the toggle button to return to its normal state.
43  * 
44  * A toggle button is created by calling either [ctor@Gtk.ToggleButton.new] or
45  * [ctor@Gtk.ToggleButton.new_with_label]. If using the former, it is advisable
46  * to pack a widget, (such as a `GtkLabel` and/or a `GtkImage`), into the toggle
47  * button’s container. (See [class@Gtk.Button] for more information).
48  * 
49  * The state of a `GtkToggleButton` can be set specifically using
50  * [method@Gtk.ToggleButton.set_active], and retrieved using
51  * [method@Gtk.ToggleButton.get_active].
52  * 
53  * To simply switch the state of a toggle button, use
54  * [method@Gtk.ToggleButton.toggled].
55  * 
56  * ## Grouping
57  * 
58  * Toggle buttons can be grouped together, to form mutually exclusive
59  * groups - only one of the buttons can be toggled at a time, and toggling
60  * another one will switch the currently toggled one off.
61  * 
62  * To add a `GtkToggleButton` to a group, use [method@Gtk.ToggleButton.set_group].
63  * 
64  * ## CSS nodes
65  * 
66  * `GtkToggleButton` has a single CSS node with name button. To differentiate
67  * it from a plain `GtkButton`, it gets the `.toggle` style class.
68  * 
69  * ## Creating two `GtkToggleButton` widgets.
70  * 
71  * ```c
72  * static void
73  * output_state (GtkToggleButton *source,
74  * gpointer         user_data)
75  * {
76  * g_print ("Toggle button "%s" is active: %s",
77  * gtk_button_get_label (GTK_BUTTON (source)),
78  * gtk_toggle_button_get_active (source) ? "Yes" : "No");
79  * }
80  * 
81  * static void
82  * make_toggles (void)
83  * {
84  * GtkWidget *window, *toggle1, *toggle2;
85  * GtkWidget *box;
86  * const char *text;
87  * 
88  * window = gtk_window_new ();
89  * box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
90  * 
91  * text = "Hi, I’m toggle button one";
92  * toggle1 = gtk_toggle_button_new_with_label (text);
93  * 
94  * g_signal_connect (toggle1, "toggled",
95  * G_CALLBACK (output_state),
96  * NULL);
97  * gtk_box_append (GTK_BOX (box), toggle1);
98  * 
99  * text = "Hi, I’m toggle button two";
100  * toggle2 = gtk_toggle_button_new_with_label (text);
101  * g_signal_connect (toggle2, "toggled",
102  * G_CALLBACK (output_state),
103  * NULL);
104  * gtk_box_append (GTK_BOX (box), toggle2);
105  * 
106  * gtk_window_set_child (GTK_WINDOW (window), box);
107  * gtk_widget_show (window);
108  * }
109  * ```
110  */
111 public class ToggleButton : Button
112 {
113 	/** the main Gtk struct */
114 	protected GtkToggleButton* gtkToggleButton;
115 
116 	/** Get the main Gtk struct */
117 	public GtkToggleButton* getToggleButtonStruct(bool transferOwnership = false)
118 	{
119 		if (transferOwnership)
120 			ownedRef = false;
121 		return gtkToggleButton;
122 	}
123 
124 	/** the main Gtk struct as a void* */
125 	protected override void* getStruct()
126 	{
127 		return cast(void*)gtkToggleButton;
128 	}
129 
130 	/**
131 	 * Sets our main struct and passes it to the parent class.
132 	 */
133 	public this (GtkToggleButton* gtkToggleButton, bool ownedRef = false)
134 	{
135 		this.gtkToggleButton = gtkToggleButton;
136 		super(cast(GtkButton*)gtkToggleButton, ownedRef);
137 	}
138 
139 
140 	/** */
141 	public static GType getType()
142 	{
143 		return gtk_toggle_button_get_type();
144 	}
145 
146 	/**
147 	 * Creates a new toggle button.
148 	 *
149 	 * A widget should be packed into the button, as in [ctor@Gtk.Button.new].
150 	 *
151 	 * Returns: a new toggle button.
152 	 *
153 	 * Throws: ConstructionException GTK+ fails to create the object.
154 	 */
155 	public this()
156 	{
157 		auto __p = gtk_toggle_button_new();
158 
159 		if(__p is null)
160 		{
161 			throw new ConstructionException("null returned by new");
162 		}
163 
164 		this(cast(GtkToggleButton*) __p);
165 	}
166 
167 	/**
168 	 * Creates a new `GtkToggleButton` containing a label.
169 	 *
170 	 * The label will be created using [ctor@Gtk.Label.new_with_mnemonic],
171 	 * so underscores in @label indicate the mnemonic for the button.
172 	 *
173 	 * Params:
174 	 *     label = the text of the button, with an underscore in front of the
175 	 *         mnemonic character
176 	 *
177 	 * Returns: a new `GtkToggleButton`
178 	 *
179 	 * Throws: ConstructionException GTK+ fails to create the object.
180 	 */
181 	public this(string label)
182 	{
183 		auto __p = gtk_toggle_button_new_with_mnemonic(Str.toStringz(label));
184 
185 		if(__p is null)
186 		{
187 			throw new ConstructionException("null returned by new_with_mnemonic");
188 		}
189 
190 		this(cast(GtkToggleButton*) __p);
191 	}
192 
193 	/**
194 	 * Queries a `GtkToggleButton` and returns its current state.
195 	 *
196 	 * Returns %TRUE if the toggle button is pressed in and %FALSE
197 	 * if it is raised.
198 	 *
199 	 * Returns: whether the button is pressed
200 	 */
201 	public bool getActive()
202 	{
203 		return gtk_toggle_button_get_active(gtkToggleButton) != 0;
204 	}
205 
206 	/**
207 	 * Sets the status of the toggle button.
208 	 *
209 	 * Set to %TRUE if you want the `GtkToggleButton` to be “pressed in”,
210 	 * and %FALSE to raise it.
211 	 *
212 	 * If the status of the button changes, this action causes the
213 	 * [signal@GtkToggleButton::toggled] signal to be emitted.
214 	 *
215 	 * Params:
216 	 *     isActive = %TRUE or %FALSE.
217 	 */
218 	public void setActive(bool isActive)
219 	{
220 		gtk_toggle_button_set_active(gtkToggleButton, isActive);
221 	}
222 
223 	/**
224 	 * Adds @self to the group of @group.
225 	 *
226 	 * In a group of multiple toggle buttons, only one button can be active
227 	 * at a time.
228 	 *
229 	 * Setting up groups in a cycle leads to undefined behavior.
230 	 *
231 	 * Note that the same effect can be achieved via the [iface@Gtk.Actionable]
232 	 * API, by using the same action with parameter type and state type 's'
233 	 * for all buttons in the group, and giving each button its own target
234 	 * value.
235 	 *
236 	 * Params:
237 	 *     group = another `GtkToggleButton` to
238 	 *         form a group with
239 	 */
240 	public void setGroup(ToggleButton group)
241 	{
242 		gtk_toggle_button_set_group(gtkToggleButton, (group is null) ? null : group.getToggleButtonStruct());
243 	}
244 
245 	/**
246 	 * Emits the ::toggled signal on the `GtkToggleButton`.
247 	 *
248 	 * There is no good reason for an application ever to call this function.
249 	 */
250 	public void toggled()
251 	{
252 		gtk_toggle_button_toggled(gtkToggleButton);
253 	}
254 
255 	/**
256 	 * Emitted whenever the `GtkToggleButton`'s state is changed.
257 	 */
258 	gulong addOnToggled(void delegate(ToggleButton) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
259 	{
260 		return Signals.connect(this, "toggled", dlg, connectFlags ^ ConnectFlags.SWAPPED);
261 	}
262 }